
/**
 * 核心处理业务类
 * @author wl.yang
 */
package com.ywl.webserver.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

import com.ywl.webserver.util.ByteUtil;
import com.ywl.webserver.util.ConfigUtil;
import com.ywl.webserver.util.PathUtil;

public class ServerCore extends Thread{
	private Socket socket;
	private InputStream in;
	private PrintStream out;
	
	public ServerCore(Socket socket){
		this.socket = socket;
		try {
			in = socket.getInputStream();
			out = new PrintStream(socket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 处理业务
	 */
	public void run(){
		//根据请求获取请求数据
		String filename = requestData(in);
		
		//根据请求路径进行响应 
		responseFile(filename);
	}

	/**
	 * 根据访问路径进行响应
	 * @param filename
	 */
	private void responseFile(String filename) {
		File file = new File(ConfigUtil.docBase+filename);
		if(!file.exists()){
			sendErrorMessage(404, "File Not Found");
			return;
		}
		
		try {
			InputStream in = new FileInputStream(file);
			byte content[] = new byte[(int)file.length()];
			in.read(content);
			
			out.println("HTTP/1.0 200 Query File");
			out.println("content-length:"+content.length);
			out.println();
			out.write(content);
			out.flush();
			out.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
	}

	/**
	 * 接收请求
	 * @param in
	 * @return
	 */
	private String requestData(InputStream in) {
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String filename = null;
		try {
			
			String str = null;
			
			//接收客户端发送过来的数据
			String httMessage = null;
			int index = 0;
			boolean flag = true;
			
			while(flag){
				str = br.readLine();
				if(index == 0){
					httMessage = str;
				}
				index++;
				if(str == null || "".equals(str)){
					flag = false;
				}
				System.out.println(str);
			}
			
			if(httMessage == null){
				sendErrorMessage(404,"file not found");
				return null;
			}
			String[] content = httMessage.split(" ");
			if(content.length!=3){
				sendErrorMessage(404,"file not found");
				return null;
			}
			
			//System.out.println("code: "+content[0]+" ,filename:"+content[1]+" ,http version:"+content[2]);
			filename = PathUtil.getFileName(content[1]);
			System.out.println(filename);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return filename;
		
	}

	private void sendErrorMessage(int errorCode, String errorMessage) {
		out.println("HTTP/1.0 "+errorCode+" "+errorMessage);
		out.println("content-type: text/html");
		out.println();
		out.println("<html>");
		out.println("<title>Error Message");
		out.println("</title>");
		out.println("<body>");
		out.println("<h1>ErrorCode:"+errorCode+",ErrorMeesage:"+errorMessage);
		out.println("</body>");
		out.println("</html>");
		out.flush();
		out.close();
		try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

